import kivy

from kivy.uix.modalview import ModalView
from kivy.uix.textinput import TextInput
from kivy.animation import Animation
from kivy.lang import Builder
from kivy.clock import Clock
from kivy.properties import ObjectProperty
from ui.fittable_widgets import FittableTextInput

Builder.load_string('''
#:import FittableLabel ui.fittable_widgets.FittableLabel
#:import FittableButton ui.fittable_widgets.FittableButton
<NumButton@FittableButton>:
    size_hint: (.33,.25)
    on_release: self.parent.parent.button_callback(self.text)
<NumPad>:
    size_hint_y: .55
    size_hint_x: 1.05
    pos_hint: {'y': -0.7}
    GridLayout:
        size_hint: .95, .93
        cols:3
        NumButton:
            text: "1"
        NumButton:
            text: "2"
        NumButton:
            text: "3"
        NumButton:
            text: "4"
        NumButton:
            text: "5"
        NumButton:
            text: "6"
        NumButton:
            text: "7"
        NumButton:
            text: "8"
        NumButton:
            text: "9"
        NumButton:
            text: "delete"
        NumButton:
            text: "0"
        NumButton:
            text: "enter"
''')


class NumInput(FittableTextInput):
    presenter = ObjectProperty()

    def __init__(self, **kwargs):
        super(NumInput, self).__init__(**kwargs)
        self.numpad = NumPad(self)

    def on_focus(self, strange_object, focus):
        self.presenter.num_pad = self.numpad
        if focus:
            if self.presenter.adapter is not None:
                self.presenter.adapter.hide_keyboard()
            self.numpad.open()
        else:
            self.numpad.dismiss()

    def on_text_validate(self):
        pass


class NumPad(ModalView):
    def __init__(self, root, **kwargs):
        self.root = root
        self.background_color = [0, 0, 0, 0]
        super(NumPad, self).__init__(**kwargs)
        #self.root.presenter.adapter.hide_keyboard()

    def button_callback(self, button_str):
        if button_str == 'delete':
            self.root.do_backspace()
            return
        if button_str == 'enter':
            self.dismiss()
            self.root.dispatch('on_text_validate')
            return
        if self.root.selection_text != '':
            self.root.selection_text = button_str
            self.root.delete_selection()
        self.root.insert_text(button_str)

    def on_touch_down(self, touch):
        if self.collide_point(*touch.pos):
            super(NumPad, self).on_touch_down(touch)
            return True
        if self.root.collide_point(*touch.pos):
            return False
        if not self.collide_point(*touch.pos):
            self.dismiss()
            return False

    def on_open(self):
        anim = Animation(pos_hint={'y': -.015}, duration=0.3)
        anim.start(self)
        self.root.focus = True

    def dismiss(self):
        anim = Animation(pos_hint={'y': -0.7}, duration=0.3)
        anim.start(self)
        self.root.focus = False
        Clock.schedule_once(self.realy_dismiss, 0.3)

    def realy_dismiss(self, time):
        self._real_remove_widget()
